package org.bootlimn;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.bootlimn.common.Stats;
import org.bootlimn.common.Process;
import org.bootlimn.parser.IoblocklogParser;
import org.bootlimn.parser.ReadwritelogParser;
import org.bootlimn.parser.StatslogParser;
import org.bootlimn.parser.ProcesslogParser;
import org.bootlimn.parser.PerprologParser;
import org.bootlimn.preprocessor.ProcTreeGenerator;

import org.bootlimn.renderer.SVGRenderer;

public class Main {

	public static void main(String[] args) {

		// My attempt to access the parsed data in main() (process)
		
		// For the processes
		List<Process> allProcesses;
		allProcesses = new ArrayList<Process>();
		
		// For  pre-processing the information to be sent to the renderer
		List<Process> uniqueP = new ArrayList<Process>();
		int level[]; 
		
		// Get the processes
		try {
			System.out.println("Parsing Process Data... ");
			ProcesslogParser spe1 = new ProcesslogParser();
			spe1.runParser();
			allProcesses = spe1.sendData();
			System.out.println("Done!\n");

		} catch (Exception e) {
			System.out.println("Could not parse process data.\n");
		}
		
		
		ProcTreeGenerator procTree = new ProcTreeGenerator();
		uniqueP = procTree.getUniqueProcesses(allProcesses);
		procTree.generateTree(uniqueP);
		procTree.convertTreeToList();
		uniqueP = procTree.getlistTree();
		level = new int[uniqueP.size()];
		level = procTree.getLevel();
		
		// Ioblocks		
		try {
			System.out.println("Parsing Ioblock Data... ");
			IoblocklogParser spe2 = new IoblocklogParser();
			spe2.runParser();
			System.out.println("Done!\n");

		} catch (Exception e) {
			System.out.println("Could not parse Ioblock data.\n");
		}
		
		
		// Reads and writes
		try {
			System.out.println("Parsing Read-Write Data... ");
			ReadwritelogParser spe3 = new ReadwritelogParser();
			spe3.runParser();
			System.out.println("Done!\n");

		} catch (Exception e) {
			System.out.println("Could not parse read write data.\n");
		}
		
		//per process CPU info
		try {
			System.out.println("Parsing per process CPU info... ");
			PerprologParser spe4 = new PerprologParser();
			spe4.runParser();
			System.out.println("Done!\n");

		} catch (Exception e) {
			System.out.println("Could not parse per process CPU info.\n");
		}
		
		
		// Get the stats
		List<Stats> myStatsMain;
		myStatsMain = new ArrayList<Stats>();
		
		try {
			System.out.println("Parsing CPU, Disk stats... ");
			StatslogParser spe5 = new StatslogParser();
			spe5.runParser();
			myStatsMain = spe5.sendData();
			System.out.println("Done!\n");

		} catch (Exception e) {
			System.out.println("Could not parse stats.\n");
		}
		

		// Create the renderer
		SVGRenderer svgrender = new SVGRenderer();
		
		// Render the image
		try {
			System.out.println("Generating SVG... ");
			svgrender.render(allProcesses, myStatsMain, uniqueP, level);
			System.out.println("Done!\n");
		} catch (IOException e) {
			System.out.println("Could not generate image.\n");
			//e.printStackTrace();
		}
		
	}

}